//  File:         Inbox.cpp
//  Author:       Scott Nordmann & Lauren Wallace
//  Description:  Non-inline member function definitions.
#include "Inbox.h"
#include "Entry.h"
#include "Note.h"
#include "Event.h"
#include "Task.h"
#include <vector>
#include <iostream>
#include <fstream>

using namespace std;
Inbox::Inbox()
{
}
Inbox::Inbox(ifstream& ifsave)
{
	int numEntries = 0;
	int ctr =0;

	Note*  tempNote;
	Task*  tempTask;
	Event* tempEvent;
	
	string tempType;
	string tempCreator; 
	string tempTitl;
	string tempCont;
	string tempDate;
	
	int    notePrio;
	double taskPrio;

	ifsave >> numEntries;
	ifsave.ignore();

	while (ctr != numEntries)
	{
		++ctr;
		getline(ifsave, tempType);
		if      (tempType == "Note")
		{
			getline(ifsave, tempCreator);
			getline(ifsave, tempTitl);
			getline(ifsave, tempCont);
			ifsave  >> notePrio;
			ifsave.ignore();
			tempNote = new Note(tempCreator,tempTitl,tempCont,notePrio);
			received.push_back(tempNote);
		}
		else if (tempType == "Task")
		{
			getline(ifsave, tempCreator);
			getline(ifsave, tempTitl);
			getline(ifsave, tempCont);
			ifsave  >> taskPrio;
			ifsave.ignore();
			tempTask = new Task(tempCreator,tempTitl,tempCont,taskPrio);
			received.push_back(tempTask);
		}
		else if (tempType == "Event")
		{
			string dateString;
			string timeString;

			getline(ifsave, dateString);
			getline(ifsave, tempCreator);
			getline(ifsave, tempTitl);
			getline(ifsave, tempCont);
			getline(ifsave, timeString);

			tempEvent = new Event(tempCreator);
			tempEvent->setDateString(dateString);
			tempEvent->setTitle(tempTitl);
			tempEvent->setContent(tempCont);
			tempEvent->setTime(timeString);

			received.push_back(tempEvent);
		}	
	}
}

void Inbox::shutDown(ofstream& ofsave)
{
	ofsave << received.size() << endl;
	if (received.size() != 0)
	{
		vector<Entry*>::iterator iter;
		for(iter=received.begin();iter !=received.end(); ++iter)
		{
			if      (dynamic_cast<Task*> (*iter) )
			{
				ofsave << "Task" << endl;
				ofsave << (*iter)->getCreatorName() << endl;
				ofsave << (*iter)->getTitle()		<< endl;
				ofsave << (*iter)->getContent()		<< endl;
				ofsave << dynamic_cast<Task*>(*iter)->getPriority()	<< endl;
			}
			else if (dynamic_cast<Event*> (*iter) )
			{			
				ofsave << "Event" << endl;
				ofsave << dynamic_cast<Event*>(*iter)->getDateString() << endl;
				ofsave << (*iter)->getCreatorName() << endl;
				ofsave << (*iter)->getTitle()       << endl;
				ofsave << (*iter)->getContent()     << endl;
				ofsave << dynamic_cast<Event*>(*iter)->getTimeString() << endl;
			}
			else 
			{			
				ofsave << "Note" << endl;
				ofsave << (*iter)->getCreatorName() << endl;
				ofsave << (*iter)->getTitle()		<< endl;
				ofsave << (*iter)->getContent()		<< endl;
				ofsave << dynamic_cast<Note*>(*iter)->getPriority()	<< endl;
			}
		}
	}
}

vector<Entry*>* Inbox::checkMail()
{
	//if you received.erase(iter) then try to keep iterating there is a run time error
	//so we break after deleting and have to start iterating over again.
	unsigned int keeping=0;//keeping stores how many entries the user has seen and wants to keep
	unsigned int ctr;//we don't ask the user about an entry till ctr is even with keeping
	char removechoice = ' ';

	vector<Entry*>::iterator iter;
	while (keeping < received.size())
	for (iter=received.begin(), ctr=0; iter!=received.end() ;++ctr, ++iter)
	{
		if (ctr >= keeping)
		{
			(*iter)->display();
			
			cout << "Would you like to keep this Entry (y/n)? " << endl << endl;
			cin >> removechoice;

			if (toupper(removechoice) == 'N')
			{
				delete (*iter);
				received.erase(iter);
				break;
			}
			else
				++keeping;

		}
		system("cls");
	}
	return &received;
}

void Inbox::sendMail(Entry* mail)
{
	sent.push_back(mail);
}